package com.desktopmng.core;

import java.io.File;
import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class JFossContext {
	private static JFossContext instance = null;

	private String contextPath = null;
	private String realPath = null;
	private String appname = null;
	private ServletContext application = null;

	// private static String[] SERVICES_CONF_FILE
	// ={"config/applicationContext.xml","config/applicationContext-components.xml"};
	// private static ApplicationContext context = new
	// ClassPathXmlApplicationContext(SERVICES_CONF_FILE);

	public static final String DEFAULT_CONTEXT = "classpath*:spring/*.xml";
	private static ApplicationContext context = new ClassPathXmlApplicationContext(
			DEFAULT_CONTEXT);

	public synchronized static JFossContext getInstance() {
		if (instance == null) {
			instance = new JFossContext();
		}
		return instance;
	}

	public ApplicationContext getContext() {
		return this.context;
	}

	public void init(ServletContext servletContext) {
		this.application = servletContext;
		// System.out.println("PortalContext:"+servletContext.getRealPath(File.separator));
		setRealPath(servletContext.getRealPath(File.separator));
	}

	public ServletContext getApplication() {
		return this.application;
	}

	/**
	 * Get context path
	 * 
	 * @return Returns the contextPath.
	 */
	public String getContextPath() {
		return contextPath;
	}

	/**
	 * Set context path
	 * 
	 * @param contextPath
	 *            The contextPath to set.
	 */
	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}

	/**
	 * Get real path
	 * 
	 * @return Returns the realPath.
	 */
	public String getRealPath() {
		// System.out.println(realPath);
		return realPath;
	}

	public String getAppName() {
		return this.appname;
	}

	/**
	 * Set real path
	 * 
	 * @param realPath
	 *            The realPath to set.
	 */
	public void setRealPath(String realPath) {
		this.realPath = realPath;
		// System.out.println("realpath ="+ realPath);
		String str = replace(this.realPath, File.separator, "#");
		String[] temp = str.split("#");
		this.appname = temp[temp.length - 1];
	}

	/**
	 * 将字符串 source 中的 oldStr 替换为 newStr, 并以大小写敏感方式进行查找
	 * 
	 * @param source
	 * @param oldStr
	 * @param newStr
	 * @return
	 */
	public static String replace(String source, String oldStr, String newStr) {
		return replace(source, oldStr, newStr, true);
	}

	/**
	 * 将字符串 source 中的 oldStr 替换为 newStr, matchCase 为是否设置大小写敏感查找
	 * 
	 * @param source
	 * @param oldStr
	 * @param newStr
	 * @param matchCase
	 * @return
	 */
	public static String replace(String source, String oldStr, String newStr,
			boolean matchCase) {
		if (source == null)
			return null;
		// 首先检查旧字符串是否存在, 不存在就不进行替换
		if (source.toLowerCase().indexOf(oldStr.toLowerCase()) == -1) {
			return source;
		}

		int findStartPos = 0;
		int a = 0;
		while (a > -1) {
			int b = 0;
			String str1, str2, str3, str4, strA, strB;
			str1 = source;
			str2 = str1.toLowerCase();
			str3 = oldStr;
			str4 = str3.toLowerCase();
			if (matchCase) {
				strA = str1;
				strB = str3;
			} else {
				strA = str2;
				strB = str4;
			}
			a = strA.indexOf(strB, findStartPos);
			if (a > -1) {
				b = oldStr.length();
				findStartPos = a + b;
				StringBuffer bbuf = new StringBuffer(source);
				source = bbuf.replace(a, a + b, newStr) + "";
				// 新的查找开始点位于替换后的字符串的结尾
				findStartPos = findStartPos + newStr.length() - b;
			}
		}
		return source;
	}
}
